/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_KOBJECT_H__
#define __EB_KOBJECT_H__


#include <types.h>
#include <list.h>
#include <string.h>


/* Collection of operations for attribute files */
struct kobj_data;


/**
 * @brief: The root object is the system's root directory, and all objects should be located below it
 * 
 * @name: directory or attribute files name
 * @kops: pointer kobj operation api
 * @parent: parent object
 * @entry: link list same level objects
 * @child: link list child all objects
 * @kref: reference count
 * @release: Release function pointer
 * @priv: Private data pointer
 */
typedef struct kobj {
    /* directory or attribute files name */
    const char        *name;
    /* pointer kobj parent */
    struct kobj       *parent;
    /* pointer brother kobj*/
    struct list_node  entry;
    /* pointer sub directory */
    struct list_node  child;
    /* reference count */
    size_t            kref;
    /* kobj attr data */
    struct kobj_data  *kdata;
    /* kobj release memory */
    void (*release)(struct kobj *kobj);
}kobj_t;


typedef struct kobj_data {
    /* kobj data region */
    void  *data;
    /* kobj data size */
    size_t data_size;
    /* kobj read */
    size_t (*read)(struct kobj *kobj, void *buf, size_t size);
    /* kobj write */
    size_t (*write)(struct kobj *kobj, void *buf, size_t size);
}kobj_data_t;


typedef size_t (*kobj_read_t)(struct kobj *kobj, void *buf, size_t size);
typedef size_t (*kobj_write_t)(struct kobj *kobj, void *buf, size_t size);


#ifdef __cplusplus
extern "C" {
#endif


/**
 * @brief Get the root object.
 * @param none
 * @return The root object.
 */
kobj_t* kobj_get_root(void);


/**
 * @brief Check whether the object is the root object.
 * @param kobj The object to check.
 * @return True if the object is the root object, otherwise false.
 */
bool kobj_is_root(kobj_t *kobj);


/**
 * @brief Check whether the object is a directory.
 * @param kobj The object to check.
 * @return True if the object is a directory, otherwise false.
 */
bool kobj_is_dir(kobj_t *kobj);


/**
 * @brief Check whether the object is an attribute.
 * @param kobj The object to check.
 * @return True if the object is an attribute, otherwise false.
 */
bool kobj_is_attr(kobj_t *kobj);


/**
 * @brief Check whether the object is a child of the parent object.
 * @param parent The parent object.
 * @param kobj The object to check.
 * @return True if the object is a child of the parent object, otherwise false.
 */
bool kobj_is_child(kobj_t *parent, kobj_t *kobj);


/**
 * @brief Initialize the root object.
 * @param none
 * @return none
 */
void kobj_root_init(void);


/**
 * @brief Initialize the object.
 * @param kobj The object to initialize.
 * @param name The name of the object.
 * @param data The data of the object.
 * @return none
 */
void kobj_init(kobj_t *kobj, const char *name, struct kobj_data *data);


/**
 * @brief Set the parent object.
 * @param kobj The object to set the parent.
 * @param parent The parent object.
 * @return none
 * @note The parent object must be initialized.
 */
void kobj_set_parent(kobj_t *kobj, kobj_t* parent);


/**
 * @brief Allocate the object.
 * @param name The name of the object.
 * @param data The data of the object.
 * @return The allocated object. NULL means failure.
 */
kobj_t* kobj_alloc(const char *name, struct kobj_data *data);


/**
 * @brief Allocate the directory object.
 * @param name The name of the directory object.
 * @return The allocated directory object.
 */
kobj_t* kobj_alloc_dir(const char *name);


/**
 * @brief Allocate the attribute object.
 * @param name The name of the attribute object.
 * @param data The data of the attribute object.
 * @return The allocated attribute object.
 */
kobj_t* kobj_alloc_attr(const char *name, struct kobj_data *data);


/**
 * @brief Add the object to the parent object.
 * @param parent The parent object.
 * @param kobj The object to add.
 * @return True if the object is added successfully, otherwise false.
 * @note The parent object must be initialized, and the kobj should be unique under the parant object.
 */
bool kobj_add(kobj_t *parent, kobj_t *kobj);


/**
 * @brief Delete the object.
 * @param kobj The object to delete.
 * @return none
 */
void kobj_delete(kobj_t *kobj);


/**
 * @brief Remove the object from the parent object.
 * @param parent The parent object.
 * @param kobj The object to remove.
 * @return True if the object is removed successfully, otherwise false.
 */
bool kobj_remove(kobj_t *parent, kobj_t *kobj);


/**
 * @brief Find the object by name.
 * @param parent The parent object.
 * @param name The name of the object.
 * @return The object found, otherwise NULL.
 */
kobj_t* kobj_find_by_name(kobj_t *parent, const char *name);


/**
 * @brief Create the directory object.
 * @param parent The parent object.
 * @param name The name of the directory object.
 * @return The created directory object.
 */
kobj_t* kobj_create_dir(kobj_t *parent, const char *name);


/**
 * @brief Create the attribute object.
 * @param parent The parent object.
 * @param name The name of the attribute object.
 * @param data The data of the attribute object.
 * @return The created attribute object.
 */
kobj_t* kobj_create_attr(kobj_t *parent, const char *name, void *data, size_t len);


/**
 * @brief Remove the object itself.
 * @param kobj The object to remove.
 * @return True if the object is removed successfully, otherwise false.
 * @note The object should not be the root object.
 */
bool kobj_remove_self(kobj_t *kobj);


/**
 * @brief Read the data of attribute object.
 * @param kobj The object to read.
 * @param buf The buffer to store the data.
 * @param size The size of the data.
 * @return The size of the data read.
 */
int kobj_attr_read(struct kobj *kobj, void *buf, size_t size);


/**
 * @brief Write the data to attribute object.
 * @param kobj The object to write.
 * @param buf The buffer to write.
 * @param size The size of the data.
 * @return The size of the data written.
 */
int kobj_attr_write(struct kobj *kobj, void *buf, size_t size);


/**
 * @brief create kobject text attribute file, it will return a kobj
 * @param parent kobject parent pointer
 * @param name attribute name
 * @param text attribute text
 * @return kobject pointer, NULL means failed
 */
#define kobj_create_text_attr(parent, name, text)  \
                    kobj_create_attr(parent, name, text, strlen(text))


#ifdef __cplusplus
}
#endif

#endif //!__EB_KOBJECT_H__
